package com.pearadmin.pro.modules.paper.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.exam.dto.request.PaperQuQueryDTO;
import com.pearadmin.pro.modules.examUtils.PagingReqDTO;
import com.pearadmin.pro.modules.examUtils.dto.BaseIdReqDTO;
import com.pearadmin.pro.modules.examUtils.dto.BaseIdRespDTO;
import com.pearadmin.pro.modules.paper.dto.ext.PaperQuDetailDTO;
import com.pearadmin.pro.modules.paper.dto.request.PaperAnswerDTO;
import com.pearadmin.pro.modules.paper.dto.request.PaperCreateReqDTO;
import com.pearadmin.pro.modules.paper.dto.request.PaperListReqDTO;
import com.pearadmin.pro.modules.paper.dto.response.ExamDetailRespDTO;
import com.pearadmin.pro.modules.paper.dto.response.ExamResultRespDTO;
import com.pearadmin.pro.modules.paper.dto.response.PaperListRespDTO;
import com.pearadmin.pro.modules.paper.service.PaperService;
import com.pearadmin.pro.modules.sys.domain.SysUser;
import com.pearadmin.pro.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 试卷控制器
 *
 * @author ZhouXingYun
 * @date 2022/5/23 17:21
 */

@Api(tags = {"试卷"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "paper")
public class PaperController extends BaseController {

    @Autowired
    private PaperService baseService;

    @Resource
    private UserContext userContext;

    @Resource
    private SysUserService sysUserService;


    /**
     * 试题详情
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "试题详情")
    @Log(title = "试题详情")
    @RequestMapping(value = "/qu-detail", method = {RequestMethod.POST})
    public Result<PaperQuDetailDTO> quDetail(@RequestBody PaperQuQueryDTO reqDTO) {
        //根据ID删除
        PaperQuDetailDTO respDTO = baseService.findQuDetail(reqDTO.getPaperId(), reqDTO.getQuId());
        return success(respDTO);
    }

    /**
     * 试卷详情
     *
     * @param reqDTO
     * @return ApiRest<ExamDetailRespDTO>
     * @author ZhouXingYun
     * @date 2022/5/23 18:30
     */
    @ApiOperation(value = "试卷详情")
    @Log(title = "试卷详情")
    @RequestMapping(value = "/paper-detail", method = {RequestMethod.POST})
    public Result<ExamDetailRespDTO> paperDetail(@RequestBody BaseIdReqDTO reqDTO) {
        //根据ID删除
        ExamDetailRespDTO respDTO = baseService.paperDetail(reqDTO.getId());
        return success(respDTO);
    }


    /**
     * 创建试卷
     *
     * @param reqDTO
     * @return ApiRest<BaseIdRespDTO>
     * @author ZhouXingYun
     * @date 2022/5/24 9:23
     */
    @ApiOperation(value = "创建试卷")
    @Log(title = "创建试卷")
    @RequestMapping(value = "/create-paper", method = {RequestMethod.POST})
    public Result<BaseIdRespDTO> save(@RequestBody PaperCreateReqDTO reqDTO) {
        //复制参数
        String paperId = baseService.createPaper(userContext.getUserId(), reqDTO.getExamId());
        return success(new BaseIdRespDTO(paperId));
    }


    /**
     * 填充答案
     * @author ZhouXingYun
     * @date 2022/5/24 16:14
     * @param reqDTO
     * @return ApiRest<PaperQuDetailDTO>
     */
    @Log(title = "填充答案")
    @ApiOperation(value = "填充答案")
    @RequestMapping(value = "/fill-answer", method = { RequestMethod.POST})
    public Result<PaperQuDetailDTO> fillAnswer(@RequestBody PaperAnswerDTO reqDTO) {
        //填充答案
        baseService.fillAnswer(reqDTO);
        return success();
    }

    /**
     * 交卷操作
     * @author ZhouXingYun
     * @date 2022/5/24 17:55
     * @param reqDTO
     * @return com.pearadmin.pro.common.web.domain.Result<com.pearadmin.pro.modules.paper.dto.ext.PaperQuDetailDTO>
     */
    @Log(title = "交卷操作")
    @ApiOperation(value = "交卷操作")
    @RequestMapping(value = "/hand-exam", method = { RequestMethod.POST})
    public Result<PaperQuDetailDTO> handleExam(@RequestBody BaseIdReqDTO reqDTO) {
        //交卷操作
        baseService.handExam(reqDTO.getId());
        return super.success();
    }

    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "我的成绩-详情")
    @Log(title = "我的成绩-详情")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public Result<IPage<PaperListRespDTO>> paging(@RequestBody PagingReqDTO<PaperListReqDTO> reqDTO) {

        //分页查询并转换
        IPage<PaperListRespDTO> page = baseService.paging(reqDTO);
        page.getRecords().forEach(paperListRespDTO -> {
            Integer state = paperListRespDTO.getState();
            if(state == 0){
                paperListRespDTO.setExamState("考试中");
            }else if(state == 1){
                paperListRespDTO.setExamState("待阅卷");
            }else if(state == 2){
                paperListRespDTO.setExamState("已考完");
            }else if(state == 3){
                paperListRespDTO.setExamState("已弃考");
            }
        });
        return super.success(page);
    }


   /**
    * 试卷结果详情
    * @author ZhouXingYun
    * @date 2022/5/26 15:05
    * @param reqDTO
    * @return ApiRest<ExamResultRespDTO>
    */
    @ApiOperation(value = "试卷结果详情")
    @Log(title = "试卷结果详情")
    @RequestMapping(value = "/paper-result", method = { RequestMethod.POST})
    public Result<ExamResultRespDTO> paperResult(@RequestBody BaseIdReqDTO reqDTO) {
        //根据ID删除
        ExamResultRespDTO respDTO = baseService.paperResult(reqDTO.getId());
        String userId = respDTO.getUserId();
        SysUser byId = sysUserService.getById(userId);
        respDTO.setNickname(byId.getNickname());
        return success(respDTO);
    }
}
